home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / X11R4 / cmds / X / ddx / Xsun / sunCG4C.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-07  |  14.0 KB  |  534 lines

  1. /*-
  2.  * sunCG4C.c --
  3.  *    Functions to support the sun CG4 board as a memory frame buffer.
  4.  */
  5.  
  6. /************************************************************
  7. Copyright 1987 by Sun Microsystems, Inc. Mountain View, CA.
  8.  
  9.                     All Rights Reserved
  10.  
  11. Permission  to  use,  copy,  modify,  and  distribute   this
  12. software  and  its documentation for any purpose and without
  13. fee is hereby granted, provided that the above copyright no-
  14. tice  appear  in all copies and that both that copyright no-
  15. tice and this permission notice appear in  supporting  docu-
  16. mentation,  and  that the names of Sun or MIT not be used in
  17. advertising or publicity pertaining to distribution  of  the
  18. software  without specific prior written permission. Sun and
  19. M.I.T. make no representations about the suitability of this
  20. software for any purpose. It is provided "as is" without any
  21. express or implied warranty.
  22.  
  23. SUN DISCLAIMS ALL WARRANTIES WITH REGARD TO  THIS  SOFTWARE,
  24. INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FIT-
  25. NESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SUN BE  LI-
  26. ABLE  FOR  ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  27. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,  DATA  OR
  28. PROFITS,  WHETHER  IN  AN  ACTION OF CONTRACT, NEGLIGENCE OR
  29. OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
  30. THE USE OR PERFORMANCE OF THIS SOFTWARE.
  31.  
  32. ********************************************************/
  33.  
  34. #ifndef    lint
  35. static char sccsid[] = "@(#)sunCG4C.c    1.4 6/1/87 Copyright 1987 Sun Micro";
  36. #endif
  37.  
  38. #include    "sun.h"
  39. #include    <sys/types.h>
  40.  
  41. #include "sys/fb.h"
  42. #include "sys/ioctl.h"
  43. #include "kernel/vmMach.h"
  44.  
  45. #include    <sys/mman.h>
  46. #ifdef sprite
  47. #undef _MAP_NEW
  48. #endif
  49.  
  50. #ifndef    sprite
  51. #include    <pixrect/memreg.h>
  52. #include    <sundev/cg4reg.h>
  53. #include    <struct.h>
  54. #endif    sprite
  55. #include    "colormap.h"
  56. #include    "colormapst.h"
  57. #include    "resource.h"
  58.  
  59. /*-
  60.  * The cg4 frame buffer is divided into several pieces.
  61.  *    1) an array of 8-bit pixels
  62.  *    2) a one-bit deep overlay plane
  63.  *    3) an enable plane
  64.  *    4) a colormap and status register
  65.  *
  66.  * XXX - put the cursor in the overlay plane
  67.  */
  68. #define    CG4_HEIGHT    900
  69. #define    CG4_WIDTH    1152
  70.  
  71. typedef struct cg4c {
  72.     u_char mpixel[128*1024];        /* bit-per-pixel memory */
  73.     u_char epixel[128*1024];        /* enable plane */
  74.     u_char cpixel[CG4_HEIGHT][CG4_WIDTH];    /* byte-per-pixel memory */
  75. } CG4C, CG4CRec, *CG4CPtr;
  76.  
  77. #define CG4C_IMAGE(fb)        ((caddr_t)(&(fb)->cpixel))
  78. #define CG4C_IMAGEOFF        ((off_t)0x0)
  79. #define CG4C_IMAGELEN        (((CG4_HEIGHT*CG4_WIDTH + 8191)/8192)*8192)
  80. #define    CG4C_MONO(fb)        ((caddr_t)(&(fb)->mpixel))
  81. #define    CG4C_MONOLEN        (128*1024)
  82. #define    CG4C_ENABLE(fb)        ((caddr_t)(&(fb)->epixel))
  83. #define    CG4C_ENBLEN        CG4C_MONOLEN
  84.  
  85. static CG4CPtr CG4Cfb = NULL;
  86.  
  87. static int  sunCG4CScreenIndex;
  88.  
  89. /* XXX - next line means only one CG4 - fix this */
  90. static ColormapPtr sunCG4CInstalledMap;
  91.  
  92. extern int TellLostMap(), TellGainedMap();
  93.  
  94. static void
  95. sunCG4CUpdateColormap(pScreen, index, count, rmap, gmap, bmap)
  96.     ScreenPtr    pScreen;
  97.     int        index, count;
  98.     u_char    *rmap, *gmap, *bmap;
  99. {
  100.     fbcmap    sunCmap;
  101.  
  102.     sunCmap.index = index;
  103.     sunCmap.count = count;
  104.     sunCmap.red = &rmap[index];
  105.     sunCmap.green = &gmap[index];
  106.     sunCmap.blue = &bmap[index];
  107.  
  108. #ifdef SUN_WINDOWS
  109.     if (sunUseSunWindows()) {
  110.     static Pixwin *pw = 0;
  111.  
  112.     if (! pw) {
  113.         if ( ! (pw = pw_open(windowFd)) )
  114.         FatalError( "sunCG4CUpdateColormap: pw_open failed\n" );
  115.         pw_setcmsname(pw, "X.V11");
  116.     }
  117.     pw_putcolormap(
  118.         pw, index, count, &rmap[index], &gmap[index], &bmap[index]);
  119.     }
  120. #endif SUN_WINDOWS
  121.  
  122.     if (ioctl(sunFbs[pScreen->myNum].fd, FBIOPUTCMAP, &sunCmap) < 0) {
  123.     perror("sunCG4CUpdateColormap");
  124.     FatalError( "sunCG4CUpdateColormap: FBIOPUTCMAP failed\n" );
  125.     }
  126. }
  127.  
  128. /*-
  129.  *-----------------------------------------------------------------------
  130.  * sunCG4CSaveScreen --
  131.  *    Preserve the color screen by turning on or off the video
  132.  *
  133.  * Results:
  134.  *    None.
  135.  *
  136.  * Side Effects:
  137.  *    Video state is switched
  138.  *
  139.  *-----------------------------------------------------------------------
  140.  */
  141. static Bool
  142. sunCG4CSaveScreen (pScreen, on)
  143.     ScreenPtr      pScreen;
  144.     Bool          on;
  145. {
  146.     int        state = on;
  147.  
  148.     switch (on) {
  149.     case SCREEN_SAVER_FORCER:
  150.     SetTimeSinceLastInputEvent();
  151.     state = 1;
  152.     break;
  153.     case SCREEN_SAVER_OFF:
  154.     state = 1;
  155.     break;
  156.     case SCREEN_SAVER_ON:
  157.     default:
  158.     state = 0;
  159.     break;
  160.     }
  161.     (void) ioctl(sunFbs[pScreen->myNum].fd, FBIOSVIDEO, &state);
  162.  
  163.     return( TRUE );
  164. }
  165.  
  166. /*-
  167.  *-----------------------------------------------------------------------
  168.  * sunCG4CCloseScreen --
  169.  *    called to ensure video is enabled when server exits.
  170.  *
  171.  * Results:
  172.  *    Screen is unsaved.
  173.  *
  174.  * Side Effects:
  175.  *    None
  176.  *
  177.  *-----------------------------------------------------------------------
  178.  */
  179. /*ARGSUSED*/
  180. static Bool
  181. sunCG4CCloseScreen(i, pScreen)
  182.     int        i;
  183.     ScreenPtr    pScreen;
  184. {
  185.     u_char rmap[256], gmap[256], bmap[256];
  186.     Bool    ret;
  187.  
  188.     pScreen->CloseScreen = (Bool (*)()) pScreen->devPrivates[sunCG4CScreenIndex].ptr;
  189.     ret = (*pScreen->CloseScreen) (i, pScreen);
  190.  
  191.     /* the following 2 lines are to fix rr clear_colormap bug */
  192.     rmap[255] = gmap[255] = bmap[255] = 0;
  193.     sunCG4CUpdateColormap(pScreen, 255, 1, rmap, gmap, bmap);
  194.  
  195.     sunCG4CInstalledMap = NULL;
  196.     (void) (*pScreen->SaveScreen) (pScreen, SCREEN_SAVER_OFF);
  197.     return ret;
  198. }
  199.  
  200. /*-
  201.  *-----------------------------------------------------------------------
  202.  * sunCG4CInstallColormap --
  203.  *    Install given colormap.
  204.  *
  205.  * Results:
  206.  *    None
  207.  *
  208.  * Side Effects:
  209.  *    Existing map is uninstalled.
  210.  *    All clients requesting ColormapNotify are notified
  211.  *
  212.  *-----------------------------------------------------------------------
  213.  */
  214. static void
  215. sunCG4CInstallColormap(cmap)
  216.     ColormapPtr    cmap;
  217. {
  218.     register int i;
  219.     register Entry *pent;
  220.     register VisualPtr pVisual = cmap->pVisual;
  221.     u_char      rmap[256], gmap[256], bmap[256];
  222.  
  223.     if (cmap == sunCG4CInstalledMap)
  224.     return;
  225.     if (sunCG4CInstalledMap)
  226.     WalkTree(sunCG4CInstalledMap->pScreen, TellLostMap,
  227.          (pointer) &(sunCG4CInstalledMap->mid));
  228.     if ((pVisual->class | DynamicClass) == DirectColor) {
  229.     for (i = 0; i < 256; i++) {
  230.         pent = &cmap->red[(i & pVisual->redMask) >>
  231.                   pVisual->offsetRed];
  232.         rmap[i] = pent->co.local.red >> 8;
  233.         pent = &cmap->green[(i & pVisual->greenMask) >>
  234.                 pVisual->offsetGreen];
  235.         gmap[i] = pent->co.local.green >> 8;
  236.         pent = &cmap->blue[(i & pVisual->blueMask) >>
  237.                    pVisual->offsetBlue];
  238.         bmap[i] = pent->co.local.blue >> 8;
  239.     }
  240.     } else {
  241.     for (i = 0, pent = cmap->red;
  242.          i < pVisual->ColormapEntries;
  243.          i++, pent++) {
  244.         if (pent->fShared) {
  245.         rmap[i] = pent->co.shco.red->color >> 8;
  246.         gmap[i] = pent->co.shco.green->color >> 8;
  247.         bmap[i] = pent->co.shco.blue->color >> 8;
  248.         }
  249.         else {
  250.         rmap[i] = pent->co.local.red >> 8;
  251.         gmap[i] = pent->co.local.green >> 8;
  252.         bmap[i] = pent->co.local.blue >> 8;
  253.         }
  254.     }
  255.     }
  256.     sunCG4CInstalledMap = cmap;
  257.     sunCG4CUpdateColormap(cmap->pScreen, 0, 256, rmap, gmap, bmap);
  258.     WalkTree(cmap->pScreen, TellGainedMap, (pointer) &(cmap->mid));
  259. }
  260.  
  261. /*-
  262.  *-----------------------------------------------------------------------
  263.  * sunCG4CUninstallColormap --
  264.  *    Uninstall given colormap.
  265.  *
  266.  * Results:
  267.  *    None
  268.  *
  269.  * Side Effects:
  270.  *    default map is installed
  271.  *    All clients requesting ColormapNotify are notified
  272.  *
  273.  *-----------------------------------------------------------------------
  274.  */
  275. static void
  276. sunCG4CUninstallColormap(cmap)
  277.     ColormapPtr    cmap;
  278. {
  279.     if (cmap == sunCG4CInstalledMap) {
  280.     Colormap defMapID = cmap->pScreen->defColormap;
  281.  
  282.     if (cmap->mid != defMapID) {
  283.         ColormapPtr defMap = (ColormapPtr) LookupIDByType(defMapID,
  284.                                   RT_COLORMAP);
  285.  
  286.         if (defMap)
  287.         (*cmap->pScreen->InstallColormap)(defMap);
  288.         else
  289.             ErrorF("sunCG4C: Can't find default colormap\n");
  290.     }
  291.     }
  292. }
  293.  
  294. /*-
  295.  *-----------------------------------------------------------------------
  296.  * sunCG4CListInstalledColormaps --
  297.  *    Fills in the list with the IDs of the installed maps
  298.  *
  299.  * Results:
  300.  *    Returns the number of IDs in the list
  301.  *
  302.  * Side Effects:
  303.  *    None
  304.  *
  305.  *-----------------------------------------------------------------------
  306.  */
  307. /*ARGSUSED*/
  308. static int
  309. sunCG4CListInstalledColormaps(pScreen, pCmapList)
  310.     ScreenPtr    pScreen;
  311.     Colormap    *pCmapList;
  312. {
  313.     *pCmapList = sunCG4CInstalledMap->mid;
  314.     return (1);
  315. }
  316.  
  317.  
  318. /*-
  319.  *-----------------------------------------------------------------------
  320.  * sunCG4CStoreColors --
  321.  *    Sets the pixels in pdefs into the specified map.
  322.  *
  323.  * Results:
  324.  *    None
  325.  *
  326.  * Side Effects:
  327.  *    None
  328.  *
  329.  *-----------------------------------------------------------------------
  330.  */
  331. static void
  332. sunCG4CStoreColors(pmap, ndef, pdefs)
  333.     ColormapPtr    pmap;
  334.     int        ndef;
  335.     xColorItem    *pdefs;
  336. {
  337.     u_char    rmap[256], gmap[256], bmap[256];
  338.     register int i;
  339.  
  340.     if (pmap != sunCG4CInstalledMap)
  341.     return;
  342.     while (ndef--) {
  343.     i = pdefs->pixel;
  344.     rmap[i] = pdefs->red >> 8;
  345.     gmap[i] = pdefs->green >> 8;
  346.     bmap[i] = pdefs->blue >> 8;
  347.     sunCG4CUpdateColormap(pmap->pScreen, i, 1, rmap, gmap, bmap);
  348.     pdefs++;
  349.     }
  350. }
  351.  
  352. /*-
  353.  *-----------------------------------------------------------------------
  354.  * sunCG4CInit --
  355.  *    Attempt to find and initialize a cg4 framebuffer used as mono
  356.  *
  357.  * Results:
  358.  *    TRUE if everything went ok. FALSE if not.
  359.  *
  360.  * Side Effects:
  361.  *    Most of the elements of the ScreenRec are filled in. Memory is
  362.  *    allocated for the frame buffer and the buffer is mapped. The
  363.  *    video is enabled for the frame buffer...
  364.  *
  365.  *-----------------------------------------------------------------------
  366.  */
  367. /*ARGSUSED*/
  368. static Bool
  369. sunCG4CInit (index, pScreen, argc, argv)
  370.     int              index;        /* The index of pScreen in the ScreenInfo */
  371.     ScreenPtr      pScreen;      /* The Screen to initialize */
  372.     int              argc;            /* The number of the Server's arguments. */
  373.     char          **argv;       /* The arguments themselves. Don't change! */
  374. {
  375.     if (!cfbScreenInit (pScreen, sunFbs[index].fb,
  376.             sunFbs[index].info.fb_width,
  377.             sunFbs[index].info.fb_height,
  378.             monitorResolution, monitorResolution,
  379.             sunFbs[index].info.fb_width))
  380.         return (FALSE);
  381.     
  382.     pScreen->SaveScreen = sunCG4CSaveScreen;
  383.     
  384.     pScreen->devPrivates[sunCG4CScreenIndex].ptr = (pointer) pScreen->CloseScreen;
  385.     pScreen->CloseScreen = sunCG4CCloseScreen;
  386.     
  387. #ifndef STATIC_COLOR
  388.     pScreen->InstallColormap = sunCG4CInstallColormap;
  389.     pScreen->UninstallColormap = sunCG4CUninstallColormap;
  390.     pScreen->ListInstalledColormaps = sunCG4CListInstalledColormaps;
  391.     pScreen->StoreColors = sunCG4CStoreColors;
  392. #endif
  393.     
  394.     sunCG4CSaveScreen( pScreen, SCREEN_SAVER_FORCER );
  395.     return (sunScreenInit(pScreen) && cfbCreateDefColormap(pScreen));
  396. }
  397.  
  398. /*-
  399.  *--------------------------------------------------------------
  400.  * sunCG4CSwitch --
  401.  *      Enable or disable color plane 
  402.  *
  403.  * Results:
  404.  *      Color plane enabled for select =0, disabled otherwise.
  405.  *
  406.  *--------------------------------------------------------------
  407.  */
  408. static void
  409. sunCG4CSwitch (pScreen, select)
  410.     ScreenPtr  pScreen;
  411.     u_char     select;
  412. {
  413.     register int    *j, *end;
  414.  
  415. /* XXX This ifndef was commented out...  I stuck it back in til overlay works */
  416. #ifndef sprite
  417.     j = (int *) fb_Addr.fb_enable;
  418.     end = j + (128 / sizeof (int)) * 1024;
  419.     if (!select)                         
  420.       while (j < end)
  421.     *j++ = 0;
  422.     else
  423.       while (j < end)
  424.     *j++ = ~0;
  425. #endif
  426. }
  427.  
  428. /*-
  429.  *-----------------------------------------------------------------------
  430.  * sunCG4CProbe --
  431.  *    Attempt to find and initialize a cg4 framebuffer used as mono
  432.  *
  433.  * Results:
  434.  *    TRUE if everything went ok. FALSE if not.
  435.  *
  436.  * Side Effects:
  437.  *    Memory is allocated for the frame buffer and the buffer is mapped.
  438.  *
  439.  *-----------------------------------------------------------------------
  440.  */
  441. /*ARGSUSED*/
  442. Bool
  443. sunCG4CProbe (pScreenInfo, index, fbNum, argc, argv)
  444.     ScreenInfo      *pScreenInfo;    /* The screenInfo struct */
  445.     int              index;        /* The index of pScreen in the ScreenInfo */
  446.     int              fbNum;        /* Index into the sunFbData array */
  447.     int              argc;            /* The number of the Server's arguments. */
  448.     char          **argv;       /* The arguments themselves. Don't change! */
  449. {
  450.     int         fd;
  451.     struct fbtype fbType;
  452. #ifdef sprite
  453.     int        sizeToUse;
  454. #endif /* sprite */
  455.  
  456.     if ((fd = sunOpenFrameBuffer(FBTYPE_SUN4COLOR, &fbType, index, fbNum,
  457.                  argc, argv)) < 0)
  458.     return FALSE;
  459.  
  460. #ifdef    _MAP_NEW
  461.     if ((int)(CG4Cfb = (CG4CPtr) mmap((caddr_t) 0,
  462.          CG4C_MONOLEN + CG4C_ENBLEN + CG4C_IMAGELEN,
  463.          PROT_READ | PROT_WRITE,
  464.          MAP_SHARED | _MAP_NEW, fd, 0)) == -1) {
  465.     Error("Mapping cg4c");
  466.     (void) close(fd);
  467.     return FALSE;
  468.     }
  469. #else    _MAP_NEW
  470. #ifdef sprite
  471.     sizeToUse = ((CG4C_MONOLEN + CG4C_ENBLEN + CG4C_IMAGELEN + VMMACH_SEG_SIZE)
  472.         & ~(VMMACH_SEG_SIZE-1)) + VMMACH_SEG_SIZE;
  473.         
  474.     CG4Cfb = (CG4CPtr) malloc(sizeToUse);
  475. #else
  476.     CG4Cfb = (CG4CPtr) valloc(CG4C_MONOLEN + CG4C_ENBLEN + CG4C_IMAGELEN);
  477. #endif /* sprite */
  478.     if (CG4Cfb == (CG4CPtr) NULL) {
  479.     ErrorF("Could not allocate room for frame buffer.\n");
  480.     return FALSE;
  481.     }
  482.  
  483. #ifdef sprite
  484.     CG4Cfb = mmap((caddr_t) CG4Cfb, CG4C_MONOLEN + CG4C_ENBLEN + CG4C_IMAGELEN,
  485.          PROT_READ | PROT_WRITE,
  486.          MAP_SHARED, fd, 0);
  487.     if (CG4Cfb == (caddr_t) NULL) {
  488. #else
  489.     if (mmap((caddr_t) CG4Cfb, CG4C_MONOLEN + CG4C_ENBLEN + CG4C_IMAGELEN,
  490.          PROT_READ | PROT_WRITE,
  491.          MAP_SHARED, fd, 0) < 0) {
  492. #endif /* sprite */
  493.     Error("Mapping cg4c");
  494.     (void) close(fd);
  495.     return FALSE;
  496.     }
  497. #endif    _MAP_NEW
  498.  
  499.     sunFbs[index].info = fbType;
  500. #ifdef sprite
  501.     sunFbs[index].fb = (pointer) CG4Cfb;
  502. #else
  503.     sunFbs[index].fb = (pointer) fb_Addr.fb_buffer;
  504. #endif /* sprite */
  505.     sunFbs[index].EnterLeave = sunCG4CSwitch;
  506.     sunSupportsDepth8 = TRUE;
  507.     sunFbs[index].fd = fd;
  508.     return TRUE;
  509. }
  510.  
  511. Bool
  512. sunCG4CCreate(pScreenInfo, argc, argv)
  513.     ScreenInfo      *pScreenInfo;
  514.     int              argc;
  515.     char          **argv;
  516. {
  517.     int i;
  518.  
  519.     if (sunGeneration != serverGeneration)
  520.     {
  521.     sunCG4CScreenIndex = AllocateScreenPrivateIndex();
  522.     if (sunCG4CScreenIndex < 0)
  523.         return FALSE;
  524.     }
  525.     i = AddScreen(sunCG4CInit, argc, argv);
  526.     if (i >= 0)
  527.     {
  528.     /* Now set the enable plane for screen 0 */
  529.     sunCG4CSwitch(pScreenInfo->screens[i], i != 0);
  530.     return TRUE;
  531.     }
  532.     return FALSE;
  533. }
  534.